package com.taptech.doufu.ui.view.topicview;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.cp.ResponseCpIndexDataBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.AppUtil;
import com.taptech.doufu.ui.activity.MoreBannerActivity;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class NovelTopicBannerView extends LinearLayout implements View.OnClickListener{
    //征文作品
    private RelativeLayout moreHotNovelBtn;
    private View collectNovelView;
    private TextView collectLeftTex;
    private TextView collectRightTex;
    private ImageView collectLeftImg;
    private ImageView collectRightImg;
    private String collectMoreUrl;
    private TextView collectTitle;
    private Context context;
    private String sexType;
    private String pageType;
    private View rl_content;

    private static final int LEFT = 1;
    private static final int RIGHT = 2;

    private EventBean eventBean;

    private ArrayList<DFHomeBannerBean> collectNovelBeanList;
    public NovelTopicBannerView(Context context) {
        super(context);
        this.context = context;
    }

    public NovelTopicBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NovelTopicBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public NovelTopicBannerView(final Context context, ResponseCpIndexDataBean.DataBean.ListBean model, final String type,int listPos,String pageType){
        this(context);
        View view = LayoutInflater.from(context).inflate(R.layout.draw_recommend_subject_layout, null);
        eventBean = new EventBean();
        eventBean.setTitle(model.getTitle());
        eventBean.setPosition(listPos);
        eventBean.setMoreType(model.getType());
        eventBean.setCardType(model.getModel());

        initViewCollectNovel(view);
        initDataCollectNovel(model);
        changeDayModelBg();
        this.sexType = type;
        this.pageType = type;
        addView(view);
    }

    public void refrush(ResponseCpIndexDataBean.DataBean.ListBean model){
        if (model != null) {
            initDataCollectNovel(model);
        }
    }




    private void initViewCollectNovel(View view) {
        moreHotNovelBtn = (RelativeLayout) view.findViewById(R.id.more_content_btn);
        moreHotNovelBtn.setOnClickListener(this);
        collectLeftTex = (TextView) view.findViewById(R.id.collect_novel_left_txt);
        collectRightTex = (TextView) view.findViewById(R.id.collect_novel_right_txt);
        collectLeftImg = (ImageView) view.findViewById(R.id.collect_novel_left_img);
        collectRightImg = (ImageView) view.findViewById(R.id.collect_novel_right_img);
        collectTitle = (TextView) view.findViewById(R.id.model_title);
        collectNovelView = view.findViewById(R.id.fg_papers_activity_layout);
        rl_content = view.findViewById(R.id.rl_content);
    }

    private void initDataCollectNovel(ResponseCpIndexDataBean.DataBean.ListBean listBean) {
        collectTitle.setText(listBean.getTitle());
        moreHotNovelBtn.setTag(listBean.getTitle());
        collectMoreUrl = listBean.getMore_url();
        collectNovelBeanList = DFHomeBannerBean.getDFHomeBannerBean(listBean.getList().toString());
        try {
            GlideUtil.displayRoundImage(context,collectLeftImg,collectNovelBeanList.get(0).getImages()[0].getImgUrl(),
                    3,R.drawable.img_default_loading_banner);


            GlideUtil.displayRoundImage(context,collectRightImg,collectNovelBeanList.get(1).getImages()[0].getImgUrl(),
                    3,R.drawable.img_default_loading_banner);
            if (collectNovelBeanList.get(0).getTitle() == null) {
                collectLeftTex.setText(collectNovelBeanList.get(0).getName());
            } else {
                collectLeftTex.setText(collectNovelBeanList.get(0).getTitle());
            }
            if (collectNovelBeanList.get(1).getTitle() == null) {
                collectRightTex.setText(collectNovelBeanList.get(1).getName());
            } else {
                collectRightTex.setText(collectNovelBeanList.get(1).getTitle());
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        collectLeftImg.setOnClickListener(this);
        collectLeftImg.setOnClickListener(this);
        collectRightTex.setOnClickListener(this);
        collectRightImg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_content_btn://更多征文活动
                UmengEventUtil.addRightTopBtnEvent(context,eventBean,sexType,pageType);
                if (collectMoreUrl != null) {
                    if(collectMoreUrl.contains("/weex/")){
                        SimpleWeexActivity.startActivity(context,collectMoreUrl);
                    }else{
                        Intent intent = new Intent(context, MoreBannerActivity.class);
                        intent.putExtra(MoreBannerActivity.MORE_URL, collectMoreUrl);
                        intent.putExtra(MoreBannerActivity.DATA_MODE_, MoreBannerActivity.DATA_MODE_CP);
                        intent.putExtra(MoreBannerActivity.MORE_TITLE, collectTitle.getText().toString());
                        context.startActivity(intent);
                    }
                }
                break;
            case R.id.collect_novel_left_txt://征文 左
            case R.id.collect_novel_left_img:
                UmengEventUtil.addContentEvent(context,eventBean,sexType,pageType);
                startCollectNovel(collectNovelBeanList, LEFT);
                break;
            case R.id.collect_novel_right_img:
            case R.id.collect_novel_right_txt:
                UmengEventUtil.addContentEvent(context,eventBean,sexType,pageType);
                startCollectNovel(collectNovelBeanList, RIGHT);
                break;
        }
    }

    /**
     * 征文活动跳转
     */
    private void startCollectNovel(ArrayList<DFHomeBannerBean> collectNovelBeans, int position) {
        if (collectNovelBeans == null && collectNovelBeans.size() < 2) {
            return;
        }
        DFHomeBannerBean collectNovelBean = null;
        if (position == LEFT) {
            collectNovelBean = collectNovelBeans.get(0);
        } else if (position == RIGHT) {
            collectNovelBean = collectNovelBeans.get(1);
        } else {

        }
        AppUtil.intentBanner(context, collectNovelBean);
    }

    public void changeDayModelBg(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg();
        }else {
            dayBg();
        }
    }

    public void dayBg() {
        if (collectNovelView != null) {
            rl_content.setBackgroundResource(R.color.white);
            collectNovelView.setBackgroundResource(R.color.home_list_item_color);
            collectLeftImg.setAlpha(1.0f);
            collectRightImg.setAlpha(1.0f);
            collectLeftTex.setTextColor(getResources().getColor(R.color.text_color_1));
            collectRightTex.setTextColor(getResources().getColor(R.color.text_color_1));
        }
    }

    public void darkBg() {
        if (collectNovelView != null) {
            rl_content.setBackgroundColor(getResources().getColor(R.color.fg_dark));
            collectNovelView.setBackgroundColor(getResources().getColor(R.color.bg_dark));
            collectLeftImg.setAlpha(Constant.imagViewAlpha);
            collectRightImg.setAlpha(Constant.imagViewAlpha);
            collectLeftTex.setTextColor(getResources().getColor(R.color.text_dark));
            collectRightTex.setTextColor(getResources().getColor(R.color.text_dark));
        }
    }
}
